#!/usr/bin/env python
# coding: utf-8

import re
import sys
from itertools import islice

GO_TYPES = ['P', 'F', 'C']

def parse_go_funcs(s):
    d = {}
    for tf in s.split():
        t,f = tf.split(':')
        assert t not in d
        d[t] = set(f.split(','))
    return d

def main_full(compare_type, real_func):
    compare_type = sys.argv[1].strip()
    assert compare_type in ['exact', 'entry']
    real_func = sys.argv[2].strip()
    predicted_funcs = (line.strip() for line in sys.stdin if line.strip())

    if not real_func:
        predicted_funcs = list(islice(predicted_funcs, 1))
        if predicted_funcs:
            # определили неизвестную функцию
            return '%s\t%s' % (-1, predicted_funcs[0])
        else:
            # не определили неизвестную функцию
            return '-2\t'
    else:
        sf1 = parse_go_funcs(real_func)
        i = -1
        best_func = None
        for i, func2 in enumerate(predicted_funcs):
            if i == 0:
                best_func = func2
            sf2 = parse_go_funcs(func2)
            if compare_type == 'entry':
                if (sf1.keys() == sf2.keys()):
                    found = True
                    for t in sf1:
                        if sf1[t].difference(sf2[t]) and sf2[t].difference(sf1[t]):
                            found = False
                    if found:
                        # угадали функцию
                        return '%s\t%s' % (i+1, func2)
            elif compare_type == 'exact':
                if sf1 == sf2:
                    # угадали функцию
                    return '%s\t%s' % (i+1, func2)
        if i < 0:
            # не предсказали никакой функции для известной
            return '0.5\t'
        else:
            # не угадали функцию
            return '%s\t%s' % (0, best_func)


def main_types(compare_type, real_func):
    compare_type = sys.argv[1].strip()
    assert compare_type in ['exact', 'entry']
    real_func = sys.argv[2].strip()
    predicted_funcs = (line.strip() for line in sys.stdin if line.strip())

    if 1:
        real_func_dict = parse_go_funcs(real_func)
        possib_best_func_dict = {}
        best_func = {}
        for i, func in enumerate(predicted_funcs):
            func_dict = parse_go_funcs(func)
            for t in func_dict:
                if t not in possib_best_func_dict:
                    possib_best_func_dict[t] = ','.join(sorted(func_dict[t]))
                if t in best_func:
                    continue
                if t not in real_func_dict:
                    best_func[t] = (-1, ','.join(sorted(func_dict[t])))
                    continue
                if compare_type == 'entry':
                    if not func_dict[t].difference(real_func_dict[t]) or not real_func_dict[t].difference(func_dict[t]):
                        # угадали функцию
                        best_func[t] = (i+1, ','.join(sorted(func_dict[t])))
                elif compare_type == 'exact':
                    if real_func_dict[t] == func_dict[t]:
                        # угадали функцию
                        best_func[t] = (i+1, ','.join(sorted(func_dict[t])))
            if set(best_func) == set(GO_TYPES):
                break

        for t in GO_TYPES:
            if t not in best_func:
                if t in real_func_dict:
                    if t in possib_best_func_dict:
                        best_func[t] = (0.5, possib_best_func_dict[t])
                    else:
                        best_func[t] = (0.5, '')
                else:
                    best_func[t] = (-2, '')


        return '%s\t%s' % (' '.join('%s:%s' % (t, best_func[t][0]) for t in GO_TYPES), ' '.join('%s:%s' % (t, best_func[t][1]) for t in GO_TYPES))




def main():
    compare_type = sys.argv[1].strip()
    assert compare_type in ['full', 'types']

    compare_func = sys.argv[2].strip()
    real_func    = sys.argv[3].strip()


    if compare_type == 'full':
        res = main_full(compare_func, real_func)
    elif compare_type == 'types':
        res = main_types(compare_func, real_func)

    print res


if __name__ == "__main__":
    main()


